package com.teampuzzle;




	import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;


public class SoundHandler {		
		public static MediaPlayer[] soundArray = new MediaPlayer[7];
	    public final static int 
	    	NEWLEVEL=0,
	    	PUZZLECOMPLETE=1,
	    	PUZZLEFAIL=2,
	    	GAMEOVER=3,
	    	DOUBLESCORE=4,
	    	STATEAWNSER=5;
	    public static int currentSound = -1;
	    
	    public static void SoundHandler(Context c){

	    	soundArray[NEWLEVEL] = MediaPlayer.create(c, R.raw.stateawnser);//Byt ut
	    	soundArray[PUZZLECOMPLETE] = MediaPlayer.create(c, R.raw.miltonwinn);
	    	soundArray[PUZZLEFAIL] = MediaPlayer.create(c, R.raw.misslyckadbana);
	    	soundArray[GAMEOVER] = MediaPlayer.create(c, R.raw.fail);
	    	soundArray[DOUBLESCORE] = MediaPlayer.create(c, R.raw.miltonplayup);//Byt ut
	    	soundArray[STATEAWNSER] = MediaPlayer.create(c, R.raw.stateawnser);//Byt ut
	    }
	    
	    public static void play(final int sound){
	    	
	    	if(currentSound == -1){ //FIX
	    		Log.d("SOUND", "START");
	    		currentSound = sound;
	    		soundArray[sound].start();
	    		Timer t = new Timer(); 
		        t.schedule(new TimerTask() {            
		        	public void run() { 
		        		currentSound = -1;
		        		//STATE_ANSWER? REMEMBER?
		        	} 
		        }, soundArray[sound].getDuration());
	    	}else{
	    		try{
		    		Timer t2 = new Timer(); 
			        t2.schedule(new TimerTask() {            
			        	public void run() { 
			        		play(sound);
			        		//STATE_ANSWER? REMEMBER?
			        	} 
			        }, soundArray[currentSound].getDuration()-soundArray[currentSound].getCurrentPosition()+15);
	    		}catch (Exception e) {}
	    	}
	    	
	    }
	    
	    public static void reset(int sound){
	    	Log.d("SOUND", "RESET");
	    	soundArray[sound].reset();
	    	try{
	    		Log.d("SOUND", "PREPARE");
	    		soundArray[sound].prepare();
	    	}
	    	catch(Exception e){}
	    }
	    
	    public static void unload(){
	    	for(int i=0; i<soundArray.length;i++){
	    		soundArray[i].release();
	    	}
	    }


		

	}
